package com.caojing.syby.ui.details.viewpagerdetail;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.caojing.syby.R;
import com.caojing.syby.base.BaseActivity;
import com.caojing.syby.bean.home.HomeGvBean;
import com.caojing.syby.bean.home.RowsBean;
import com.caojing.syby.config.UrlConfig;
import com.caojing.syby.ui.adapter.HomeGvAdapter;
import com.caojing.syby.ui.details.productdetail.ClickGridviewItemActivity;
import com.caojing.syby.view.MyGridView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;

public class VpDetailActivity extends BaseActivity implements VpDetailContract.View {


    @Bind(R.id.VpDetail_tvTitle)
    TextView VpDetailTvTitle;
    @Bind(R.id.VpDetail_iv)
    ImageView VpDetailIv;
    @Bind(R.id.VpDetail_gv)
    MyGridView VpDetailGv;
    @Bind(R.id.VpDetail_sv)
    PullToRefreshScrollView VpDetailSv;
    private VpDetailContract.Presenter presenter;
    private List<RowsBean> totallist;
    private HomeGvAdapter adapter;
    private int PAGE = 1;
    private String actid;
    private String headurl;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_vp_detail;
    }

    @Override
    protected void initView() {
        VpDetailSv.scrollTo(0,0);
        presenter = new VpDetailPresenter(this);
        Intent intent = getIntent();
        headurl = intent.getStringExtra("headurl");
        String address = intent .getStringExtra("address");
        actid = address.substring(address.length()-6,address.length()-4);
        String name = intent.getStringExtra("name");
        VpDetailTvTitle.setText(name);
        Log.i("TAG",actid);
    }

    @Override
    protected void initData() {

        Glide.with(this).load(headurl).placeholder(R.mipmap.jky_bg).error(R.mipmap.jky_bg).into(VpDetailIv);
        VpDetailSv.scrollTo(0,0);

        totallist = new ArrayList<>();
        adapter = new HomeGvAdapter(totallist,this);
        VpDetailGv.setAdapter(adapter);

        presenter.initGvData(initParams());

        VpDetailSv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                VpDetailSv.onRefreshComplete();
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                PAGE++;
                presenter.initGvData(initParams());
                //Log.i("TAG","参数"+initParams().get(UrlConfig.Key.ACTID));
            }
        });
    }

    private HashMap<String,String> initParams() {
        HashMap<String,String> params = new HashMap<>();
        params.put(UrlConfig.Key.ACT, UrlConfig.DefaultValue.ACT);
        params.put(UrlConfig.Key.V, UrlConfig.DefaultValue.V);
        params.put(UrlConfig.Key.PAGES, PAGE+"");
        params.put(UrlConfig.Key.ACTID,actid);
       /* params.put(UrlConfig.Key.BC, UrlConfig.DefaultValue.BC);
        params.put(UrlConfig.Key.SC, UrlConfig.DefaultValue.SC);
        params.put(UrlConfig.Key.SORTS,UrlConfig.DefaultValue.SORTS);
        *//*&channel=0&ckey=&daynews=&lprice=0&hprice=0&tbclass=0&actid=18&brandid=0&predate=&index=0*//*
        params.put(UrlConfig.Key.CHANNEL,UrlConfig.DefaultValue.CHANNEL);
        params.put(UrlConfig.Key.CKEY, UrlConfig.DefaultValue.CKEY);
        params.put(UrlConfig.Key.DAYNEWS, UrlConfig.DefaultValue.DAYNEWS);
        params.put(UrlConfig.Key.LPRICE,UrlConfig.DefaultValue.LPRICE);
        params.put(UrlConfig.Key.HPRICE,UrlConfig.DefaultValue.HPRICE);
        params.put(UrlConfig.Key.TBCLASS,UrlConfig.DefaultValue.TBCLASS);

        params.put(UrlConfig.Key.BRANDID,UrlConfig.DefaultValue.BRANDID);
        params.put(UrlConfig.Key.PREDATE,"");
        params.put(UrlConfig.Key.INDEX,"0");*/
        return params;
    }

    @Override
    public void onSuccessGetGv(HomeGvBean homeGvBean) {
        List<RowsBean> list = homeGvBean.getRows();
        totallist.addAll(list);
        adapter.notifyDataSetChanged();
        VpDetailSv.onRefreshComplete();
       // Log.i("TAG","view成功"+homeGvBean.getRows().size());
        VpDetailGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(VpDetailActivity.this, ClickGridviewItemActivity.class);
                RowsBean rowsBean = totallist.get(position);
                if (rowsBean != null) {
                    String spreadUrl = rowsBean.getSpreadUrl();
                    intent.putExtra("spreadUrl", spreadUrl);
                    String name = rowsBean.getName();
                    intent.putExtra("name",name);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void onFailGetGv(String msg) {
        Toast.makeText(VpDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

}
